package com.softeem.score.manager.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.softeem.score.manager.mapper.GradeMapper;
import com.softeem.score.manager.service.GradeService;
import com.softeem.score.model.dto.gradeDto.AdminScoreQueryDTO;
import com.softeem.score.model.dto.gradeDto.StudentScoreQueryDTO;
import com.softeem.score.model.enmu.CourseBelongingEnum;
import com.softeem.score.model.enmu.CourseCategoryEnum;
import com.softeem.score.model.enmu.CourseNatureEnum;
import com.softeem.score.model.vo.educational.GradeVo;
import com.softeem.score.model.vo.educational.StatisticVo;
import com.softeem.score.model.vo.educational.StudentScoreVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2024-02-14
 */
@Service
public class GradeServiceImpl implements GradeService {
    @Autowired
    private GradeMapper gradeMapper;

    @Override
    public PageInfo<GradeVo> findByPage(AdminScoreQueryDTO adminScoreQueryDTO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GradeVo> gradeVoList = gradeMapper.findByPage(adminScoreQueryDTO);
        PageInfo<GradeVo> pageInfo = new PageInfo<>(gradeVoList);
        return pageInfo;
    }

    @Override
    public PageInfo<StudentScoreVo> findByPageByStudentNumber(StudentScoreQueryDTO studentScoreQueryDTO, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudentScoreVo> gradeVoList = gradeMapper.findByStudentNumber(studentScoreQueryDTO);
        for (StudentScoreVo studentScoreVo : gradeVoList) {
            studentScoreVo.setCategoryName(CourseCategoryEnum.getById(studentScoreVo.getCategory()).getValue());
            studentScoreVo.setNatureName(CourseNatureEnum.getById(studentScoreVo.getNature()).getValue());
            studentScoreVo.setOwnershipName(CourseBelongingEnum.getById(studentScoreVo.getOwnership()).getValue());
        }
        return new PageInfo<>(gradeVoList);
    }

    @Override
    public Map<String, Object> findAllGradeByCourseId(Long courseId) {
        List<StatisticVo> allGradeByCourseId = gradeMapper.findAllGradeByCourseId(courseId);
        // 构建响应结果数据
        Map<String, Object> resultMap = new HashMap<>();
        List<String> category = new ArrayList<>();
        List<BigDecimal> averageData = new ArrayList<>();
        List<BigDecimal> maxData = new ArrayList<>();
        List<BigDecimal> minData = new ArrayList<>();
        List<Integer> failData = new ArrayList<>();
        for (StatisticVo statisticVo : allGradeByCourseId) {
            if (statisticVo.getAcademicYear() == null) {
                continue;
            }
            category.add(statisticVo.getAcademicYear() + "级");
            averageData.add(statisticVo.getAverageData());
            maxData.add(statisticVo.getMaxData());
            minData.add(statisticVo.getMinData());
            failData.add(statisticVo.getFailData());
        }
        resultMap.put("category", category);
        resultMap.put("averageData", averageData);
        resultMap.put("maxData", maxData);
        resultMap.put("minData", minData);
        resultMap.put("failData", failData);
        return resultMap;
    }
}
